package com.tgpms.web.element.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tgpms.common.PageUtils;
import com.tgpms.common.Query;
import com.tgpms.web.element.dao.WsDocDao;
import com.tgpms.web.element.dao.WsDocRelDao;
import com.tgpms.web.element.entity.WsDoc;
import com.tgpms.web.element.entity.WsDocRel;
import com.tgpms.web.element.service.WsDocRelService;
import com.tgpms.web.element.service.WsDocService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("wsDocService")
public class WsDocServiceImpl extends ServiceImpl<WsDocDao, WsDoc> implements WsDocService {
    @Autowired
    private WsDocRelDao wsDocRelDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WsDoc> page = this.page(
                new Query<WsDoc>().getPage(params),
                // 拼接查询条件
                new LambdaQueryWrapper<WsDoc>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils getWsDocByDocRelId(String docRelId,Map<String, Object> params) {
        // 查询附件关联表
        List<WsDocRel> wsDocRels = wsDocRelDao.selectList(new LambdaQueryWrapper<WsDocRel>().eq(WsDocRel::getDocRelId, docRelId));
        if (CollectionUtils.isEmpty(wsDocRels)) {
            return null;
        }
        // 获取附件id
        List<String> ids = wsDocRels.stream().map(WsDocRel::getId).collect(Collectors.toList());
        // 查询附件表
        IPage<WsDoc> page = this.baseMapper.selectPage(new Query<WsDoc>().getPage(params), new LambdaQueryWrapper<WsDoc>().in(WsDoc::getId, ids));
        return new PageUtils(page);
    }

}